local qizuo = fk.CreateSkill {
  name = "ls__qizuo",
}

Fk:loadTranslationTable{
  ["ls__qizuo"] = "奇佐",
  [":ls__qizuo"] = "当你受到伤害后，可发现一张锦囊牌并交给任意角色。若你于此轮已/未发动过”知机“的摸牌效果，你因发现/卜算观看的牌数永久+1。",
  ["#ls__qizuo-discover"] = "奇佐：发现一张锦囊牌",
  ["#ls__qizuo-give"] = "奇佐：将%arg交给一名角色",
  ["@ls__qizuo_discover"] = "奇佐：发现",
  ["@ls__qizuo_guanxing"] = "奇佐：卜算",

  ["$ls__qizuo1"] = "智者，当有高瞻远瞩，推演时局之能。",
  ["$ls__qizuo2"] = "奉孝在此，丞相何忧？",
}

local ls = require "packages/lvshi/utility"

-- 受到伤害后发现锦囊牌
qizuo:addEffect(fk.Damaged, {
  anim_type = "masochism",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(qizuo.name)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local current_count = player:getMark("@ls__qizuo_discover")
    -- 发现一张锦囊牌
    local cards = ls.discover(player, "trick", current_count + 3)
    if #cards == 0 then return end
    local choosed_cards, _ = room:askToChooseCardsAndChoice(player, {
      cards = cards,
      choices = {"OK"},
      skill_name = qizuo.name,
      prompt = "#ls__qizuo-discover",
      min_num = 1,
      max_num = 1,
      all_cards = cards
    })
    
    local targets = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = room:getAlivePlayers(),
      skill_name = qizuo.name,
      prompt = "#ls__qizuo-give:::" .. Fk:getCardById(choosed_cards[1]):toLogString(),
      cancelable = false,
      no_indicate = true,
    })
    
    if #targets > 0 then
      room:obtainCard(targets[1], choosed_cards[1], true, fk.ReasonGive, player, qizuo.name)
      -- 检查本轮是否触发了知机的摸牌时机并增加观看牌数统计
      if player:getMark("@@zhiji_triggered-round") > 0 then
        room:addPlayerMark(player, "@ls__qizuo_discover")
      else
        room:addPlayerMark(player, "@ls__qizuo_guanxing")
      end
    end
  end,
})

return qizuo